const express = require('express');
const path = require('path');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
const port = 80;

// 静态文件服务
app.use(express.static(path.join(__dirname)));

const domain = 'http://127.0.0.1:88';
// API 代理配置
app.use('/api', createProxyMiddleware({
    target: `${domain}/api`,
    changeOrigin: true,
}));

// uploads 代理配置
app.use('/uploads', createProxyMiddleware({
    target: `${domain}/uploads`,
    changeOrigin: true,
}));

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'pages/index.html'));
});

// 其他页面路由要放在代理配置后面
app.get('/:page', (req, res) => {
    res.sendFile(path.join(__dirname, `pages/${req.params.page}`));
});

app.listen(port, () => {
    console.log(`Front-end server is running on http://localhost:${port}`);
});

